Conditions | 11 |
Paths | 176 |
Total Lines | 154 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like Search.query often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /* |
||
317 | query : function(query) { |
||
318 | var i; |
||
319 | var stopwords = ["a","and","are","as","at","be","but","by","for","if","in","into","is","it","near","no","not","of","on","or","such","that","the","their","then","there","these","they","this","to","was","will","with"]; |
||
320 | |||
321 | // stem the searchterms and add them to the correct list |
||
322 | var stemmer = new Stemmer(); |
||
323 | var searchterms = []; |
||
324 | var excluded = []; |
||
325 | var hlterms = []; |
||
326 | var tmp = query.split(/\s+/); |
||
327 | var objectterms = []; |
||
328 | for (i = 0; i < tmp.length; i++) { |
||
329 | if (tmp[i] !== "") { |
||
330 | objectterms.push(tmp[i].toLowerCase()); |
||
331 | } |
||
332 | |||
333 | if ($u.indexOf(stopwords, tmp[i].toLowerCase()) != -1 || tmp[i].match(/^\d+$/) || |
||
334 | tmp[i] === "") { |
||
335 | // skip this "word" |
||
336 | continue; |
||
337 | } |
||
338 | // stem the word |
||
339 | var word = stemmer.stemWord(tmp[i].toLowerCase()); |
||
340 | var toAppend; |
||
341 | // select the correct list |
||
342 | if (word[0] == '-') { |
||
343 | toAppend = excluded; |
||
344 | word = word.substr(1); |
||
345 | } |
||
346 | else { |
||
347 | toAppend = searchterms; |
||
348 | hlterms.push(tmp[i].toLowerCase()); |
||
349 | } |
||
350 | // only add if not already in the list |
||
351 | if (!$u.contains(toAppend, word)) |
||
352 | toAppend.push(word); |
||
353 | } |
||
354 | var highlightstring = '?highlight=' + $.urlencode(hlterms.join(" ")); |
||
355 | |||
356 | // console.debug('SEARCH: searching for:'); |
||
357 | // console.info('required: ', searchterms); |
||
358 | // console.info('excluded: ', excluded); |
||
359 | |||
360 | // prepare search |
||
361 | var terms = this._index.terms; |
||
362 | var titleterms = this._index.titleterms; |
||
363 | |||
364 | // array of [filename, title, anchor, descr, score] |
||
365 | var results = []; |
||
366 | $('#search-progress').empty(); |
||
367 | |||
368 | // lookup as object |
||
369 | for (i = 0; i < objectterms.length; i++) { |
||
370 | var others = [].concat(objectterms.slice(0, i), |
||
371 | objectterms.slice(i+1, objectterms.length)); |
||
372 | results = results.concat(this.performObjectSearch(objectterms[i], others)); |
||
373 | } |
||
374 | |||
375 | // lookup as search terms in fulltext |
||
376 | results = results.concat(this.performTermsSearch(searchterms, excluded, terms, Scorer.term)) |
||
377 | .concat(this.performTermsSearch(searchterms, excluded, titleterms, Scorer.title)); |
||
378 | |||
379 | // let the scorer override scores with a custom scoring function |
||
380 | if (Scorer.score) { |
||
381 | for (i = 0; i < results.length; i++) |
||
382 | results[i][4] = Scorer.score(results[i]); |
||
383 | } |
||
384 | |||
385 | // now sort the results by score (in opposite order of appearance, since the |
||
386 | // display function below uses pop() to retrieve items) and then |
||
387 | // alphabetically |
||
388 | results.sort(function(a, b) { |
||
389 | var left = a[4]; |
||
390 | var right = b[4]; |
||
391 | if (left > right) { |
||
392 | return 1; |
||
393 | } else if (left < right) { |
||
394 | return -1; |
||
395 | } else { |
||
396 | // same score: sort alphabetically |
||
397 | left = a[1].toLowerCase(); |
||
398 | right = b[1].toLowerCase(); |
||
399 | return (left > right) ? -1 : ((left < right) ? 1 : 0); |
||
400 | } |
||
401 | }); |
||
402 | |||
403 | // for debugging |
||
404 | //Search.lastresults = results.slice(); // a copy |
||
405 | //console.info('search results:', Search.lastresults); |
||
406 | |||
407 | // print the results |
||
408 | var resultCount = results.length; |
||
409 | function displayNextItem() { |
||
410 | // results left, load the summary and display it |
||
411 | if (results.length) { |
||
412 | var item = results.pop(); |
||
413 | var listItem = $('<li style="display:none"></li>'); |
||
414 | if (DOCUMENTATION_OPTIONS.FILE_SUFFIX === '') { |
||
415 | // dirhtml builder |
||
416 | var dirname = item[0] + '/'; |
||
417 | if (dirname.match(/\/index\/$/)) { |
||
418 | dirname = dirname.substring(0, dirname.length-6); |
||
419 | } else if (dirname == 'index/') { |
||
420 | dirname = ''; |
||
421 | } |
||
422 | listItem.append($('<a/>').attr('href', |
||
423 | DOCUMENTATION_OPTIONS.URL_ROOT + dirname + |
||
424 | highlightstring + item[2]).html(item[1])); |
||
425 | } else { |
||
426 | // normal html builders |
||
427 | listItem.append($('<a/>').attr('href', |
||
428 | item[0] + DOCUMENTATION_OPTIONS.FILE_SUFFIX + |
||
429 | highlightstring + item[2]).html(item[1])); |
||
430 | } |
||
431 | if (item[3]) { |
||
432 | listItem.append($('<span> (' + item[3] + ')</span>')); |
||
433 | Search.output.append(listItem); |
||
434 | listItem.slideDown(5, function() { |
||
435 | displayNextItem(); |
||
436 | }); |
||
437 | } else if (DOCUMENTATION_OPTIONS.HAS_SOURCE) { |
||
438 | $.ajax({url: DOCUMENTATION_OPTIONS.URL_ROOT + '_sources/' + item[0] + '.txt', |
||
439 | dataType: "text", |
||
440 | complete: function(jqxhr, textstatus) { |
||
441 | var data = jqxhr.responseText; |
||
442 | if (data !== '' && data !== undefined) { |
||
443 | listItem.append(Search.makeSearchSummary(data, searchterms, hlterms)); |
||
444 | } |
||
445 | Search.output.append(listItem); |
||
446 | listItem.slideDown(5, function() { |
||
447 | displayNextItem(); |
||
448 | }); |
||
449 | }}); |
||
450 | } else { |
||
451 | // no source available, just display title |
||
452 | Search.output.append(listItem); |
||
453 | listItem.slideDown(5, function() { |
||
454 | displayNextItem(); |
||
455 | }); |
||
456 | } |
||
457 | } |
||
458 | // search finished, update title and status message |
||
459 | else { |
||
460 | Search.stopPulse(); |
||
461 | Search.title.text(_('Search Results')); |
||
462 | if (!resultCount) |
||
463 | Search.status.text(_('Your search did not match any documents. Please make sure that all words are spelled correctly and that you\'ve selected enough categories.')); |
||
464 | else |
||
465 | Search.status.text(_('Search finished, found %s page(s) matching the search query.').replace('%s', resultCount)); |
||
466 | Search.status.fadeIn(500); |
||
467 | } |
||
468 | } |
||
469 | displayNextItem(); |
||
470 | }, |
||
471 | |||
622 | }); |